﻿@model IEnumerable<DiscussProject.Models.Article>
@{
    Layout = "~/Views/Shared/_ArticleLayout.cshtml";
}

@section showinfo{
    <cite class="layui-hide-xs">@ViewBag.User.Name</cite>
    <img src="@Session["head_img"]">
}


<div class="fly-panel fly-column">
    <div class="layui-container">
        <ul class="layui-clear">
            <li class="layui-this act actss"><a class="chooses">首页</a></li>
            <li class="layui-hide-xs act"><a class="choose" id="taolun" value="讨论">讨论</a></li>
            <li class="layui-hide-xs act"><a class="choose" id="xueshu" value="学术">学术</a></li>
            <li class="layui-hide-xs act"><a class="choose" id="yule" value="娱乐">娱乐</a></li>
            <li class="layui-hide-xs act"><a class="choose" id="jiaoyou" value="交友">交友</a></li>
            <li class="layui-hide-xs act"><a class="choose" id="fenxiang" value="分享">分享</a></li>
            <li class="layui-hide-xs layui-hide-sm layui-show-md-inline-block"><span class="fly-mid"></span></li>

            <!-- 用户登入后显示 -->
            <li class="layui-hide-xs layui-hide-sm layui-show-md-inline-block"><a href="/User/Index">我发表的贴</a></li>
        </ul>

        <div class="fly-column-right layui-hide-xs">
            <div class="layui-input-inline">
                <input type="text" ame="vercode" id="sear" placeholder="输入名字搜索帖子" autocomplete="off" class="layui-input">
            </div>
            <span><i class="layui-icon searchs"></i></span>
            <a href="/Articles/Create2" class="layui-btn">发表新帖</a>
        </div>
        <div class="layui-hide-sm layui-show-xs-block" style="margin-top: -10px; padding-bottom: 10px; text-align: center;">

        </div>

    </div>
</div>

<div class="layui-container">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md8">
            <div class="fly-panel" style="margin-bottom: 0;">

                <ul class="fly-list" id="containers">
                </ul>

                <!-- <div class="fly-none">没有相关数据</div> -->
            </div>
        </div>
        <div class="layui-col-md4">
            <div class="fly-panel fly-link">
                <h3 class="fly-panel-title">小组人员</h3>
                <dl class="fly-panel-main">
                    <dd><a href="#" target="_blank">周浩峰</a>
                    <dd>
                    <dd><a href="#" target="_blank">李铭轩</a>
                    <dd>
                    <dd><a href="#" target="_blank">李焯豪</a>
                    <dd>
                    <dd><a href="#" target="_blank">张泽华</a></dd>
                    <dd><a href="#" target="_blank">戴玮</a>
                    <dd>
                    <dd><a href="#" target="_blank">吴勋宇</a>
                    <dd>
                </dl>
            </div>

        </div>
    </div>
</div>
@Scripts.Render("~/bundles/jquery")

<script>
    $(function () {
        let urlAll = '/Articles/FindAll'
        $.ajax({
            url: urlAll,
            success(res) {
                let group = res
                let str = '';
                if (group.length === 0) {
                    str = `<li>
                                <div class="fly-list-info" style="text-align:center;">暂无人发帖</div>
                          </li>`
                } else {
                    for (let i = 0; i < group.length; i++) {
                        str += ` <li>
                            <a href="user/home.html" class="fly-avatar">
                                <img src="${group[i].Author.Picture}" alt="${group[i].Author.Name}">
                            </a>
                            <h2>
                                <a class="layui-badge">${group[i].Category}</a>
                                <a href="Detail2?id=${group[i].Id}">${group[i].Title}</a>
                            </h2>
                            <div class="fly-list-info">
                                <a href="user/home.html" link>
                               ${(function () {
                                if (group[i].Author != null) {
                                    return `<cite>${group[i].Author.Name}</cite>`;
                                } else {
                                    return ' <cite>无</cite>';
                                }
                            })()
                            }
                                </a>
                                <span>${DateHelper(group[i].CreateTime)}</span>
                            </div>
                        </li>`
                    }
                }
                $('#containers').html(str);
            }
        })

    })
    //搜索
    $(".searchs").click(function (event) {
        event.stopPropagation();
        let vale = $('#sear').val()
        $('#sear').val('')
        $.ajax({
            url: '/Articles/FindByTitleLike',
            data: {
                title: vale
            },
            success(res) {
                let group = res
                let str = '';
                if (group.length === 0) {
                    alert("没有该贴，请重新搜索")
                    window.location.reload();
                } else {
                    $('li').siblings('.act').removeClass('layui-this');
                    $('li').siblings('.act').addClass('layui-hide-xs');
                    $('.actss').removeClass('layui-hide-xs');
                    $('.actss').addClass('layui-this');
                    for (let i = 0; i < group.length; i++) {
                        str += ` <li>
                            <a href="user/home.html" class="fly-avatar">
                                <img src="${group[i].Author.Picture}" alt="${group[i].Author.Name}">
                            </a>
                            <h2>
                                <a class="layui-badge">${group[i].Category}</a>
                                <a href="Detail2?id=${group[i].Id}">${group[i].Title}</a>
                            </h2>
                            <div class="fly-list-info">
                                <a href="user/home.html" link>
                               ${(function () {
                                if (group[i].Author != null) {
                                    return `<cite>${group[i].Author.Name}</cite>`;
                                } else {
                                    return ' <cite>无</cite>';
                                }
                            })()
                            }
                                </a>
                                <span>${DateHelper(group[i].CreateTime)}</span>
                            </div>
                        </li>`
                    }
                }
                $('#containers').html(str);

            }

        })
    })
    $(".chooses").click(function () {
        $('li').siblings('.act').removeClass('layui-this');
        $('li').siblings('.act').addClass('layui-hide-xs');
        $(this).parent().removeClass('layui-hide-xs');
        $(this).parent().addClass('layui-this');
        let urlAll = '/Articles/FindAll'
        $.ajax({
            url: urlAll,
            success(res) {
                let group = res
                let str = '';
                if (group.length === 0) {
                    str = `<li>
                                <div class="fly-list-info" style="text-align:center;">暂无人发帖</div>
                          </li>`
                } else {
                    for (let i = 0; i < group.length; i++) {
                        str += ` <li>
                            <a href="user/home.html" class="fly-avatar">
                                <img src="${group[i].Author.Picture}" alt="${group[i].Author.Name}">
                            </a>
                            <h2>
                                <a class="layui-badge">${group[i].Category}</a>
                                <a href="Detail2?id=${group[i].Id}">${group[i].Title}</a>
                            </h2>
                            <div class="fly-list-info">
                                <a href="user/home.html" link>
                               ${(function () {
                                if (group[i].Author != null) {
                                    return `<cite>${group[i].Author.Name}</cite>`;
                                } else {
                                    return ' <cite>无</cite>';
                                }
                            })()
                            }
                                </a>
                                <span>${DateHelper(group[i].CreateTime)}</span>
                            </div>
                        </li>`
                    }
                }
                $('#containers').html(str);
            }
        })
    })
    let url = "/Articles/FindByCategory"
    $('.choose').click(function (e) {
        $('li').siblings('.act').removeClass('layui-this');
        $('li').siblings('.act').addClass('layui-hide-xs');
        $(this).parent().removeClass('layui-hide-xs');
        $(this).parent().addClass('layui-this');
        let chooses = $(this).attr("value")
        $.ajax({
            url: url,
            data: {
                category: chooses
            },
            success(res) {
                console.log(res)
                let group = res
                let str = '';
                if (group.length === 0) {
                    str = `<li>
                                <div class="fly-list-info" style="text-align:center;">该分区暂无人发帖</div>
                          </li>`
                } else {
                    for (let i = 0; i < group.length; i++) {
                        str += ` <li>
                            <a href="user/home.html" class="fly-avatar">
                                <img src="${group[i].Author.Picture}" alt="${group[i].Author.Name}">
                            </a>
                            <h2>
                                <a class="layui-badge">${group[i].Category}</a>
                                <a href="Detail2?id=${group[i].Id}">${group[i].Title}</a>
                            </h2>
                            <div class="fly-list-info">
                                <a href="user/home.html" link>
                               ${(function () {
                                if (group[i].Author != null) {
                                    return `<cite>${group[i].Author.Name}</cite>`;
                                } else {
                                    return ' <cite>无</cite>';
                                }
                            })()
                            }
                                </a>
                                <span>${DateHelper(group[i].CreateTime)}</span>
                            </div>
                        </li>`
                    }
                }
                $('#containers').html(str);
            }
        })
    })
    function DateHelper(dateStr) {
        //转化为时间类型
        var obj = new Date(parseInt(dateStr.replace("/Date(", "").replace(")/", ""), 10));
        //格式化为友好的格式
        var sre = obj.getUTCFullYear() + '-' + obj.getUTCMonth() + '-' + obj.getUTCDate() + "  " + obj.getUTCHours() + ':' + obj.getUTCMinutes()
        return sre
    }
</script>
<script src="~/Scripts/layui.js"></script>
<script>
    console.log(layui)
    layui.config({
        version: "2.0.0"
    }).extend({
        fly: '../../Scripts/index'
    }).use('fly');
</script>


